package com.itranswarp.mydemo02;


import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

public class Client {
    public static void main(String[] args) {
        Ordinal ordinal = new Ordinal();
        ordinal.eat();
        ordinal.run();
        ordinal.write();

        InvocationHandler handler = (proxy , method , handlerArgs) ->{
            //定义新的eat方法
            if ("eat".equals(method.getName())){
                System.out.println("村长儿子,得吃香的喝辣的");
                return null;
            }
            if ("write".equals(method.getName())){
                System.out.println("我的作文题目,是 <我的区长父亲>.");
                method.invoke(ordinal , handlerArgs);
                System.out.println("比赛获取了一等奖, so easy1");
                return null;
            }
            return null;
        };
        Student proxyInstance = (Student)Proxy.newProxyInstance(ordinal.getClass().getClassLoader(), ordinal.getClass().getInterfaces(), handler);
        proxyInstance.eat();
        proxyInstance.write();
        proxyInstance.run();

    }

}

interface Student{
    void eat();
    void run();
    void write();
}
class Ordinal implements Student{

    @Override
    public void eat() {
        System.out.println("韭菜爱吃饭饭");
    }

    @Override
    public void run() {
        System.out.println("韭菜爱运动");
    }

    @Override
    public void write() {
        System.out.println("韭菜在写作文");
    }
}